//
//  FMLocalAudioTool.m
//  FMHome
//
//  Created by apple on 17/4/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FMLocalAudioTool.h"

@interface FMLocalAudioTool ()

/** 音乐播放器 */
@property (nonatomic ,strong) AVAudioPlayer  *player;

@property (nonatomic, copy) void(^stateBlock)(BOOL isPlaying);

@end

@implementation FMLocalAudioTool

- (void)playAudioWithPath:(NSURL *)filePath stateBlock:(void (^)(BOOL))stateBlock{
    self.stateBlock = stateBlock;
    
    if ([filePath isEqual:self.player.url]) {
        [self.player play];
        stateBlock(self.player.isPlaying);
        return ;
    }
    
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
    self.player.enableRate = YES;
    [self.player prepareToPlay];
    [self.player play];
    if (self.stateBlock) {
        self.stateBlock(self.player.isPlaying);
    }
}


- (void)playCurrentAudio{
    
    [self.player play];
    if (self.stateBlock) {
        self.stateBlock(self.player.isPlaying);
    }
}

- (void)pauseCurrentAudio {
    [self.player pause];
    if (self.stateBlock) {
        self.stateBlock(self.player.isPlaying);
    }
}

- (float)currentPlayTime{
    return self.player.currentTime;
}

- (float)currentTotalTime{
    return self.player.duration;
}

- (void)seekBack: (unsigned int)time{
    self.player.currentTime -= time;
}

- (void)seekFast: (unsigned int)time{
    self.player.currentTime += time;
}

- (void)seekProgress: (float)progress{
    self.player.currentTime = self.player.duration * progress;
}

- (void)changeRate: (float)rate{
    self.player.rate = rate;
}


- (BOOL)isPlaying {
    return self.player.isPlaying;
}

@end
